﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FXnaGis;

using FXnaInfrastructure;

using System.Runtime.Serialization.Formatters.Binary;

using System.Runtime.Serialization;

using FXnaMet;

using ASAermod;




namespace ASCore
{

    /// <summary>
    /// 用于保存核心的输入数据
    /// </summary>
    [Serializable]
    public class ASData
    {

        public ASData()
        {


            this.RegisterEvents();



            //提供默认的初始化参数

            Pollution So2 = new Pollution("SO2");
            So2.Enabled = true;



            this._polltions.Add(So2);


           




 
        }



        /// <summary>
        /// 订阅内部事件
        /// </summary>
        protected void RegisterEvents()
        {

            this._polltions.EntityReseted += new EventHandler(_polltions_EntityReseted);
 
        }




        void _polltions_EntityReseted(object sender, EventArgs e)
        {
            //循环所有的污染源啊，并对所做的修改进行更新

            foreach (FShape shp in this._ptLayer.Shapes.Values)
            {

                this.ResetPollutions(shp);

            }


            foreach (FShape shp in this._lnLayer.Shapes.Values)
            {
                this.ResetPollutions(shp);
            }



         





          
        }


        /// <summary>
        /// 辅助函数
        /// </summary>
        /// <param name="shp"></param>
        void ResetPollutions(FShape shp)
        {

            BaseSource source = shp as BaseSource;

            source.ResetPollutions(this._polltions);


 
        }



        #region 气象数据


        //这里我们先使用普通的方式存储,这里主要是为了测试运行项目，所以之后这里必须要更改，下面的数据都是临时的




        MetSurfFile _surfFile;

        public MetSurfFile SurfFile
        {
            get { return _surfFile; }
            set { _surfFile = value; }
        }

        MetUpperAirFile _upperAirFile;

        public MetUpperAirFile UpperAirFile
        {
            get { return _upperAirFile; }
            set { _upperAirFile = value; }
        }




        DateTime _startTime=DateTime.Now;

        /// <summary>
        /// 运行的开始时间
        /// </summary>
        public DateTime StartTime
        {
            get { return _startTime; }
            set { _startTime = value; }
        }




        DateTime _endTime=DateTime.Now;

        /// <summary>
        /// 运行的结束时间
        /// </summary>
        public DateTime EndTime
        {
            get { return _endTime; }
            set { _endTime = value; }
        }





        #endregion



        #region  污染物



        FEntityCollection _polltions = new FEntityCollection();

        /// <summary>
        /// 用于定义污染物的集合
        /// </summary>
        public FEntityCollection Polltions
        {
            get { return _polltions; }

           
        }

        #endregion








        #region 污染源


        FVectorLayer _ptLayer = new FVectorLayer(ASCoreNames.PointSourceLayer);

        public FVectorLayer PtLayer
        {
            get { return _ptLayer; }

        }
      


        FVectorLayer _lnLayer = new FVectorLayer(ASCoreNames.LineSourceLayer);

        /// <summary>
        /// 线源
        /// </summary>
        public FVectorLayer LnLayer
        {
            get { return _lnLayer; }
            
        }



        FVectorLayer cubeLayer = new FVectorLayer(ASCoreNames.CubeSourceLayer);

        public FVectorLayer CubeLayer
        {
            get { return cubeLayer; }
           
        }



        FVectorLayer rectLayer = new FVectorLayer(ASCoreNames.RectSourceLayer);

        public FVectorLayer RectLayer
        {
            get { return rectLayer; }
           
        }



        FVectorLayer polyLayer = new FVectorLayer(ASCoreNames.PolySourceLayer);

        public FVectorLayer PolyLayer
        {
            get { return polyLayer; }
           
        }




        FVectorLayer circleLayer = new FVectorLayer(ASCoreNames.CircleSourceLayer);

        public FVectorLayer CircleLayer
        {
            get { return circleLayer; }
          
        }




        FVectorLayer expLayer = new FVectorLayer(ASCoreNames.ExpSourceLayer);

        public FVectorLayer ExpLayer
        {
            get { return expLayer; }

        }



        #endregion


        #region 接受体与建筑物

        //定义其他图层,网格图层

        FVectorLayer _receptorLayer = new FVectorLayer(ASCoreNames.RectSourceLayer);

        public FVectorLayer ReceptorLayer
        {
            get { return _receptorLayer; }
           
        }




        FVectorLayer _gridLayer = new FVectorLayer(ASCoreNames.GridLayer);

        public FVectorLayer GridLayer
        {
            get { return _gridLayer; }
            
        }




        FVectorLayer _buildings = new FVectorLayer(ASCoreNames.BuildingLayers);

        public FVectorLayer Buildings
        {
            get { return _buildings; }

        }


        #endregion


        #region  关于序列化


        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {


            this.RegisterEvents();

          




        }

        [OnSerializing]
        public void OnSerializing(StreamingContext context)
        {




        }


        #endregion

        #region 关于运行计算底层转换


        public AermapData GetAermap()
        {
            AermapData map = new AermapData();

            return map;

        }


        public AermetData GetAermet()
        {

            //临时测试数据

            this._startTime = new DateTime(2008,1,2);

            this._endTime = new DateTime(2008, 1, 10);




            AermetData met = new AermetData();

            met.UpperAirData =ASCoreNames.UpperAirData;

            met.SurfData = ASCoreNames.SurfData;

            met.StartTime = this.StartTime;
            met.EndTime = this.EndTime;






            return met;

        }

        public AermodData GetAermod(string pollutionName)
        {

            AermodData mod = new AermodData();

            return mod;

        }



        public List<AermodModel> GetAermodModel()
        {
            List<AermodModel> lst = new List<AermodModel>();




            //需要根据污染源的数量，进行第一次划分，这里也许我们可以做一些适当的优化处理

            AermodModel am = new AermodModel();


            //关于aermet 数据

            

            //关于aermap数据


            //aermod数据的封装，多污染物的思考



            foreach (FEntity item in this._polltions.Entities)
            {

                Pollution p = item as Pollution;

                

                
            }





            return lst;

        }
        


        #endregion


    }
}
